<%-- Created by IntelliJ IDEA. --%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
  <title>办公楼门禁管理系统</title>
</head>
<body>
<%
  if (request.getAttribute("list") == null) {
    request.getRequestDispatcher("IndexServlet").forward(request, response);
  }
%>
<section>
  <h2>办公管理系统</h2>
  <button>办卡</button>
  <table>
    <tr align="center" style="background-color: #B7B7B9">
      <th>持卡人姓名</th>
      <th>持卡人身份证号</th>
      <th>启用状态</th>
      <th>办卡日期</th>
      <th>操作</th>
    </tr>
    <c:forEach items="${list}" var="item" varStatus="status">
      <tr style="background-color: ${status.index % 2 == 0 ? '':'#D8D8DB'}">
        <td>${item.userName}</td>
        <td>${item.idNumber}</td>
        <td>${item.status == 1 ? "启用":"禁用"}</td>
        <td>${item.createDate}</td>
        <td>
          <a href="javascript:void(0);"
             onclick="modify(${item.status},${item.id})">${item.status == 0 ? "启用":"禁用"}
          </a>
          &nbsp;
          <a href="javascript:void(0);" onclick="delCard(${item.id})">删除</a>
        </td>
      </tr>
    </c:forEach>
  </table>
  <strong>${info}</strong>
</section>
<script src="js/jquery-1.12.4.js"></script>
<script>
  var info = "";
  $("button").click(function () {
    location.href = "add.jsp";
  })

  function modify(no, id) {
    if (no == 1) {
      var flag = confirm("是否禁用门禁卡？")
      if (flag) {
        $.post("ModifyServlet", {"status": 0, "id": id}, function () {
          location.href = "index.jsp";
        })
      }
    } else {
      var flag = confirm("是否启用门禁卡？")
      if (flag) {
        $.post("ModifyServlet", {"status": 1, "id": id}, function () {
          location.href = "index.jsp";
        })
      }
    }
  }

  function delCard(id) {
    var flag = confirm("确定要删除吗")
    if (flag) {
      $.post("DeleteServlet", {"id": id}, function (data) {
        if (data == "1") {
          alert("删除成功")
        } else {
          alert("删除失败")
        }
        location.href = "index.jsp";
      })
    }
  }
</script>
</body>
</html>